package com.luyuan.crm.sms.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.model.DataModel;

import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.sms.Constant;
import com.luyuan.crm.sms.model.SmsLogModel;
import com.luyuan.crm.sms.service.SmsLogService;
import com.luyuan.crm.sms.service.SmsService;
import com.ruijie.components.setting.service.SettingService;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;
import com.ruijie.foundation.query.criteria.RestrictionTemplate;
import com.ruijie.web.jsf.action.BaseAction;
import com.ruijie.web.jsf.context.TableContext;
import com.ruijie.web.jsf.model.PageDataModel;
import com.ruijie.web.jsf.model.TableOrder;
import com.ruijie.web.jsf.model.TablePage;

@Component("smsAction")
@Scope("view")
public class SmsAction extends BaseAction {

    @Autowired
    private SmsService smsService;

    @Autowired
    private SettingService settingService;

    @Autowired
    private SmsLogService logService;

    private Date startDate;

    private Date endDate;

    private String username;

    private String password;

    private String signature;

    private String toPhone;

    private String smsContent1;

    private String smsContentChangeExpress;

    private String smsContent2;

    private String smsContent3;

    private SmsLogModel criteria = new SmsLogModel();

    public SmsLogModel getCriteria() {
        return criteria;
    }

    public DataModel getTableModel() {
        TablePage limit = TableContext.getTablePage();
        List<TableOrder> tableOrders = TableContext.getTableOrders();
        List<Order> orders = new ArrayList<Order>();
        for (TableOrder tableOrder : tableOrders) {
            Order order = tableOrder.toOrder();
            if (order != null) {
                orders.add(order);
            }
        }
        orders.add(Order.desc("sendDate"));
        CriteriaTemplate c = CriteriaTemplate.forClass(SmsLogModel.class);
        RestrictionTemplate r = new RestrictionTemplate(true);
        c.add(r.like("fromUsername", criteria.getFromUsername()));
        c.add(r.like("content", criteria.getContent()));
        c.add(r.gt("sendDate", startDate));
        c.add(r.lt("sendDate", endDate));
        Page page = this.logService.pagedQuery(c, limit.getPage(), limit.getPageSize(), orders);
        return new PageDataModel(page);
    }

    public void sendMessage(String phone, String content, String spnumber) {
        this.smsService.sendMessage(phone, content);
        this.addGlobalI18nMessage("短信已发送，是否发送成功请查看日志");
    }

    public void update() {
        this.settingService.updateSetting(Constant.CP_ID, this.username);
        this.settingService.updateSetting(Constant.CP_PWD, this.password);
        this.settingService.updateSetting(Constant.CP_SIGN, this.signature);
        this.addGlobalI18nMessage("修改成功");
    }

    public void updateSmsContent() {
        this.settingService.updateSetting(Constant.SMS_CONTENT_SHIPPING, this.smsContent1);
        this.settingService.updateSetting(Constant.SMS_CONTENT_RECEIVE, this.smsContent2);
        this.settingService.updateSetting(Constant.SMS_CONTENT_SALES, this.smsContent3);
        this.settingService.updateSetting(Constant.SMS_CONTENT_CHANGE_EXPRESS, this.smsContentChangeExpress);
        this.addGlobalI18nMessage("修改成功");
    }

    public void sendTest() {
        this.smsService.sendMessage(toPhone, "来自路远客服系统的测试短信。");
        this.addGlobalI18nMessage("已经发送测试信息，是否发送成功请查看日志");
    }

    public String getUsername() {
        if (username == null || "".equals(username)) {
            this.username = (this.settingService.getSetting(Constant.CP_ID)).getValue();
        }
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        if (password == null || "".equals(password)) {
            this.password = (this.settingService.getSetting(Constant.CP_PWD)).getValue();
        }
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSignature() {
        if (signature == null || "".equals(signature)) {
            signature = (settingService.getSetting(Constant.CP_SIGN)).getValue();
        }
        return signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public void setToPhone(String toPhone) {
        this.toPhone = toPhone;
    }

    public String getToPhone() {
        return toPhone;
    }

    public void setSmsService(SmsService smsService) {
        this.smsService = smsService;
    }

    public String getSmsContent1() {
        if (smsContent1 == null || "".equals(smsContent1)) {
            smsContent1 = (settingService.getSetting(Constant.SMS_CONTENT_SHIPPING)).getValue();
        }
        return smsContent1;
    }

    public void setSmsContent1(String smsContent1) {
        this.smsContent1 = smsContent1;
    }

    public String getSmsContent2() {
        if (smsContent2 == null || "".equals(smsContent2)) {
            smsContent2 = (settingService.getSetting(Constant.SMS_CONTENT_RECEIVE)).getValue();
        }
        return smsContent2;
    }

    public void setSmsContent2(String smsContent2) {
        this.smsContent2 = smsContent2;
    }

    public String getSmsContent3() {
        if (smsContent3 == null || "".equals(smsContent3)) {
            smsContent3 = (settingService.getSetting(Constant.SMS_CONTENT_SALES)).getValue();
        }
        return smsContent3;
    }

    public void setSmsContent3(String smsContent3) {
        this.smsContent3 = smsContent3;
    }

    public String getSmsContentChangeExpress() {
        if (smsContentChangeExpress == null || "".equals(smsContentChangeExpress)) {
            smsContentChangeExpress = (settingService.getSetting(Constant.SMS_CONTENT_CHANGE_EXPRESS)).getValue();
        }
        return smsContentChangeExpress;
    }

    public void setSmsContentChangeExpress(String smsContentChangeExpress) {
        this.smsContentChangeExpress = smsContentChangeExpress;
    }
}
